//
//  ChatHelper.m
//  EaseMob
//
//  Created by 王方伟 on 16/7/20.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "ChatHelper.h"
#import <EMSDK.h>

@interface ChatHelper ()<EMContactManagerDelegate, EMChatManagerDelegate, EMGroupManagerDelegate, EMClientDelegate>

@end

@implementation ChatHelper

+ (instancetype)sharedChatHelper{
    static ChatHelper *chatHelper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        chatHelper = [[ChatHelper alloc] init];
    });
    return chatHelper;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    //注册好友回调
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
}

- (void)dealloc{
    //移除好友回调
    [[EMClient sharedClient].contactManager removeDelegate:self];
    [[EMClient sharedClient].chatManager removeDelegate:self];
    [[EMClient sharedClient] removeDelegate:self];
}

#pragma mark 用户A发送加用户B为好友的申请，用户B会收到这个回调
- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername message:(NSString *)aMessage{
    if (!aUsername) return;
    self.contactsVC.tabBarItem.badgeValue = @"1";
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"好友添加请求" message:aMessage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
            EMError *error = [[EMClient sharedClient].contactManager declineInvitationForUsername:aUsername];
            if (!error) {
                MyLog(@"发送拒绝成功");
            }
        }];
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"接受" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
            EMError *error = [[EMClient sharedClient].contactManager acceptInvitationForUsername:aUsername];
            if (!error) {
                MyLog(@"发送同意成功");
                [self.contactsVC reloadContactData];
            }
        }];
    }];
    [alertVC addAction:actionA];
    [alertVC addAction:actionB];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark 用户A发送加用户B为好友的申请，用户B同意后，用户A会收到这个回调
- (void)didReceiveAgreedFromUsername:(NSString *)aUsername{
    NSString *message = [NSString stringWithFormat:@"%@ 同意了你的请求", aUsername];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"添加成功" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.contactsVC reloadContactData];
    }];
    [alertVC addAction:action];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark 用户A发送加用户B为好友的申请，用户B拒绝后，用户A会收到这个回调
- (void)didReceiveDeclinedFromUsername:(NSString *)aUsername{
    NSString *message = [NSString stringWithFormat:@"%@ 拒绝了你的请求", aUsername];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"被拒绝" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:action];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark 用户B删除与用户A的好友关系后，用户A会收到这个回调
- (void)didReceiveDeletedFromUsername:(NSString *)aUsername{
    [self.contactsVC getDataFromService];
}

#pragma mark 接收消息
- (void)didReceiveMessages:(NSArray *)aMessages{
    if (_chatVC) {//正在聊天界面
        [_chatVC didReceiveMessages:aMessages];
    }else{//未读消息
        [_conversationVC refrshUnreadMessages];
    }
}

#pragma mark 会话列表发生变化
- (void)didUpdateConversationList:(NSArray *)aConversationList{
    [_conversationVC didUpdateConversationList:aConversationList];
}


#pragma mark 重连
- (void)didConnectionStateChanged:(EMConnectionState)aConnectionState{
    self.conversationVC.state = aConnectionState;
}

@end
